//
//  TestListenAlertView.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/6/7.
//

#import "TestListenAlertView.h"

@implementation TestListenAlertView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    self.backgroundColor = UIColorHex_(0x000000,0.4);
    __weak typeof(self)self_weak_ = self;
    NSString *title = @"现在开始听力测试，中途不可暂停";
    NSString *highlightedText = @"不可暂停";
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:title];
    // 设置默认文本属性
    NSDictionary *defaultAttributes = @{
        NSForegroundColorAttributeName:KFontBlackColor,
        NSFontAttributeName:JPUIFontPingFangSCRegular(16)
    };
    [attributedText setAttributes:defaultAttributes range:NSMakeRange(0, attributedText.length)];
    // 设置高亮文本属性
    NSDictionary *highlightAttributes = @{
        NSForegroundColorAttributeName:UIColorHex(0xFE4A4B),
        NSFontAttributeName:JPUIFontPingFangSCRegular(16)
    };
    NSRange highlightedRange = [title rangeOfString:highlightedText];
    if (highlightedRange.location != NSNotFound) {
        [attributedText setAttributes:highlightAttributes range:highlightedRange];
    }
    CGFloat h = [self getStringHeightWithText:title font:JPUIFontPingFangSCRegular(16) viewWidth:210];
    if(h < 44){
        h = 44;
    }
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@280);
        make.centerX.mas_equalTo(self_weak_);
        make.height.mas_equalTo(118+h);
        make.centerY.mas_equalTo(self_weak_);
    }];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.numberOfLines = 0;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.attributedText = attributedText;
    [bgView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@210);
        make.top.mas_equalTo(@35);
        make.centerX.mas_equalTo(bgView);
    }];
    
    UIButton *sureBtn = [[UIButton alloc] init];
    [sureBtn setTitle:@"我知道了" forState:UIControlStateNormal];
    [sureBtn setTitleColor:KFontGrayColor forState:UIControlStateNormal];
    sureBtn.titleLabel.font = JPUIFontPingFangSCRegular(16);
    [sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(@0);
        make.height.mas_equalTo(@48);
    }];
    // 禁用按钮
    sureBtn.enabled = NO;
    __block NSTimeInterval countdownTime = 5.0;
    NSString *normalText = @"我知道了";
    __block NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        countdownTime -= 0.1;
        dispatch_async(dispatch_get_main_queue(), ^{
            if (countdownTime <= 0) {
                // 倒计时结束，停止定时器并启用按钮
                [timer invalidate];
                sureBtn.enabled = YES;
                [sureBtn setTitle:normalText forState:UIControlStateNormal];
            } else {
                // 更新按钮文本
                NSString *countdownDisplayText = countdownTime >= 1.0 ? [NSString stringWithFormat:@"我知道了（%.0fs）", countdownTime] : [NSString stringWithFormat:@"我知道了（%.0fs）",1.0];
                [sureBtn setTitle:countdownDisplayText forState:UIControlStateNormal];
            }
        });
    }];

    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];



    UIView *line = [[UIView alloc]init];
    line.backgroundColor = KLineColor;
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(@-48.5);
        make.height.mas_equalTo(0.5);
        make.left.right.mas_equalTo(@0);
    }];
    
}

-(void)sureBtnClick{
    if(_knowBlock){
        _knowBlock();
    }
}

@end
